package com.x.reader.dao;

import com.x.framework.Base;
import com.x.framework.dao.BaseDao;
import com.x.reader.model.Paper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class PaperDao extends BaseDao {

    private final static String selectPaperListSql = "select t1.* from reader_paper t1, reader_category t2 where t1.category_id = t2.category_id ";

    public List<Paper> selectPaperList(Paper paper) throws Exception {
        StringBuilder sb = new StringBuilder(selectPaperListSql);
        List params = new ArrayList();
        if (Base.isNotNull(paper.getPaperId())) {
            sb.append("and t1.paper_id = ? ");
            params.add(paper.getPaperId());
        }
        if (Base.isNotNull(paper.getCategoryId())) {
            sb.append("and t1.category_id = ? ");
            params.add(paper.getCategoryId());
        }
        if (Base.isNotNull(paper.getPaperTitle())) {
            sb.append("and (t1.paper_title like ? or t1.paper_author like ?) ");
            params.add("%" + paper.getPaperTitle() + "%");
            params.add("%" + paper.getPaperTitle() + "%");
        }
        return this.queryForList(sb.toString(), Paper.class, params, paper);
    }
}
